package com.fcai.microserv.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@ApiModel(value="服务返回结果", description="API接口的返回对象")
@Setter(AccessLevel.PRIVATE)
@Getter
public class ServiceResult<T> implements Serializable {
    @ApiModelProperty("响应代码")
    private String code;

    @ApiModelProperty(value = "响应消息", notes = "不建议用作显示给用户的信息")
    private String msg;

    @ApiModelProperty("具体的返回业务对象")
    private T data;

    private ServiceResult() {
    }

    private ServiceResult(ServiceRespModel respCode) {
        this.code = respCode.getCode();
        this.msg = respCode.getMessage();
    }

    public ServiceResult<T> data(T data) {
        this.setData(data);
        return this;
    }

    public ServiceResult<T> code(String code) {
        this.setCode(code);
        return this;
    }

    public ServiceResult<T> msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    public static <T> ServiceResult<T> result(ServiceRespModel respCode) {
        return new ServiceResult<>(respCode);
    }

    public static <T> ServiceResult<T> result(String code, String msg) {
        return new ServiceResult<T>().code(code).msg(msg);
    }

    public static <T> ServiceResult<T> success() {
        return result(CommonRespCode.SUCCESS);
    }

    public static <T> ServiceResult<T> success(T data) {
        ServiceResult<T> result = result(CommonRespCode.SUCCESS);
        result.data(data);
        return result;
    }

    public static <T> ServiceResult<T> error() {
        return result(CommonRespCode.ERROR);
    }

    public static <T> ServiceResult<T> error(String msg) {
        ServiceResult<T> result = result(CommonRespCode.ERROR);
        result.msg(msg);
        return result;
    }

}
